home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / delicious_bookmarks-2.0.64-fx.xpi / chrome / deliciousBookmarks.jar / content / idManager.js < prev    next >
Encoding:
JavaScript  |  2008-06-19  |  12.9 KB  |  371 lines

  1. var gYBLoginWindow = null;
  2.  
  3. var YBidManager = {
  4.  
  5.   _syncService : Components.classes["@mozilla.org/ybookmarks-sync-service;1"].
  6.               getService(Components.interfaces.nsIYBookmarkSyncService),
  7.   _windowMediator : Components.classes["@mozilla.org/appshell/window-mediator;1"].
  8.                        getService(Components.interfaces.nsIWindowMediator),
  9.   _localStoreService : Components.classes["@mozilla.org/ybookmarks-store-service;1"].
  10.                           getService(Components.interfaces.nsIYBookmarksStoreService),
  11.   _socialStoreService : Components.classes["@yahoo.com/socialstore/delicious;1"].
  12.                              getService(Components.interfaces.nsISocialStore),                          
  13.   _prefs : Components.classes["@mozilla.org/preferences-service;1"].
  14.              getService(Components.interfaces.nsIPrefBranch),
  15.  
  16.    setLastUsername: function( username ) {
  17.       yDebug.print( "Last user name set to " + username );
  18.  
  19.       this._prefs.setCharPref(
  20.          "extensions.ybookmarks@yahoo.login.last", username);
  21.    },
  22.  
  23.   observe: function( subject, topic, data ) {      
  24.     try {
  25.         if (topic == "ybookmark.userChanged" && data == "silentlogout") {
  26.             this._silentLogout();
  27.             return;
  28.        }
  29.        else if ( data == "loggedin" ) {
  30.             //Find re-login / User change.
  31.             var userChanged = false;        
  32.             var del = Components.classes["@yahoo.com/socialstore/delicious;1"]
  33.                      .getService( Components.interfaces.nsISocialStore );      
  34.             var username = del.getUserName();
  35.             var lastLogin = null;            
  36.             try {
  37.               lastLogin = this._prefs.getCharPref("extensions.ybookmarks@yahoo.login.last");
  38.             } catch (e) {}
  39.             userChanged = (username != lastLogin);
  40.             if (userChanged) {
  41.                 
  42.                this._loginChanged();
  43.             } else {
  44.                this._loggedIn();
  45.             }        
  46.         } else if ( data == "loggedout") {
  47.             this._loggedOut();
  48.             return;
  49.         } else if (data == "cookie_expired") {
  50.             this._cookieExpired();
  51.             return;
  52.         }
  53.     } catch (e) {
  54.         yDebug.print("idManager.js::observe()=>Exception: "+e,YB_LOG_MESSAGE);
  55.     }
  56.   },
  57.  
  58.   _loggedIn: function() {
  59.       yDebug.print("idManager.js::YBidManager::_loggedIn",YB_LOG_MESSAGE);
  60.     var recentWindow = 
  61.       this._windowMediator.getMostRecentWindow("navigator:browser");
  62.     var del = Components.classes["@yahoo.com/socialstore/delicious;1"]
  63.                      .getService( Components.interfaces.nsISocialStore );      
  64.     var username = del.getUserName();
  65.  
  66.     this.setLastUsername(username);
  67.  
  68.     ybookmarksMain.setLoggedInState(username);
  69.  
  70.     if (this._isEngineInstalled() ) {
  71.       if (recentWindow == window) {
  72.         setTimeout(function(syncService, socialStoreService) { 
  73.                      syncService.allowSync(); 
  74.                      syncService.sync(true);
  75.                      socialStoreService.allowImportPolling();
  76.                       
  77.                    }, 2000, this._syncService, this._socialStoreService);
  78.       }
  79.     }
  80.   },
  81.  
  82.   _loginChanged: function() {
  83.       yDebug.print("idManager.js::YBidManager::_loginChanged",YB_LOG_MESSAGE);
  84.     var recentWindow =
  85.       this._windowMediator.getMostRecentWindow("navigator:browser");
  86.     var del = Components.classes["@yahoo.com/socialstore/delicious;1"]
  87.                  .getService( Components.interfaces.nsISocialStore );      
  88.     var username = del.getUserName();
  89.     if (username) {
  90.       this.setLastUsername(username);
  91.       ybookmarksMain.setLoggedInState(username);
  92.     }
  93.     else {
  94.       ybookmarksMain.setLoggedOutState();
  95.     }
  96.     
  97.     if (this._isEngineInstalled() ) { 
  98.       if (recentWindow == window) {
  99.         this._cancelSync();
  100.         if (username) {
  101.           this._removeUsersBookmarksStore();
  102.           setTimeout(function(syncService, socialStoreService) { 
  103.                        syncService.allowSync(); 
  104.                        syncService.sync(true);
  105.                        socialStoreService.allowImportPolling();
  106.                      }, 2000, this._syncService, this._socialStoreService);
  107.         }
  108.       }
  109.     }
  110.   },
  111.  
  112.     
  113.     _silentLogout: function( ) {
  114.       yDebug.print("idManager.js::YBidManager::_silentLogout",YB_LOG_MESSAGE);
  115.  
  116.     ybookmarksMain.setLoggedOutState();
  117.     
  118.     if (this._isEngineInstalled() ) {
  119.         this._cancelSync();
  120.         this._socialStoreService.disallowImportPolling();
  121.         //Keep local copy of bookmarks, We ask user via the above dialog to keep/remove
  122.         YBidManager.keepLocalContent();
  123.         this._localStoreService.setLastUpdateTime("-1");
  124.         this._localStoreService.flush(true);
  125.     }
  126.   },
  127.   
  128.   _loggedOut: function( ) {
  129.       yDebug.print("idManager.js::YBidManager::_loggedOut",YB_LOG_MESSAGE);
  130.     var recentWindow =
  131.       this._windowMediator.getMostRecentWindow("navigator:browser");
  132.  
  133.     ybookmarksMain.setLoggedOutState();
  134.     
  135.     if(ybookmarksUtils.getExtensionMode() == YB_EXTENSION_MODE_CLASSIC)  {
  136.         //No need to show loggedout dialog in classic mode.
  137.         return;
  138.     }
  139.     
  140.     if (this._isEngineInstalled() ) {
  141.       if (recentWindow == window) {    
  142.         var rv = {  remove : false };
  143.         
  144.         this._cancelSync();
  145.         this._socialStoreService.disallowImportPolling();
  146.         yDebug.print("idManager.js::YBidManager::_loggedOut=>Opening keep local content dialog.",YB_LOG_MESSAGE);
  147.         window.openDialog( "chrome://ybookmarks/content/loggedOut.xul",
  148.                        "LoggedOut",
  149.                            "chrome,dialog,centerscreen,modal,resizable=no", rv);        
  150.         //Keep local copy of bookmarks, We ask user via the above dialog to keep/remove
  151.         YBidManager.keepLocalContent();
  152.         if (rv.remove) {
  153.           this._removeUsersBookmarksStore();
  154.         }
  155.         else {
  156.           //force a partial sync if the same user login again
  157.           this._localStoreService.setLastUpdateTime("-1");
  158.           this._localStoreService.flush(true);
  159.         }
  160.       }
  161.     }
  162.   },
  163.   
  164.   _cookieExpired: function() {
  165.     var recentWindow =
  166.       this._windowMediator.getMostRecentWindow("navigator:browser");
  167.     ybookmarksMain.setLoggedOutState();    
  168.     if (this._isEngineInstalled() ) {
  169.       if (recentWindow == window) {
  170.         this._cancelSync();
  171.         this._socialStoreService.disallowImportPolling();
  172.         YBidManager.keepLocalContent();
  173.         //force a partial sync if the same user login again
  174.         this._localStoreService.setLastUpdateTime("-1");
  175.         this._localStoreService.flush(true);
  176.       }
  177.     }          
  178.   },
  179.   
  180.   _isEngineInstalled : function() {
  181.       var installed = false;
  182.       var strings = document.getElementById("ybookmarks-strings");
  183.       var newVersionNum = strings.getString("extensions.ybookmarks.versionNum");
  184.           yDebug.print("idManager.js::YBidManager::_isEngineInstalled()=>newVersionNum= "+newVersionNum,
  185.               YB_LOG_MESSAGE);
  186.       
  187.       try {
  188.           var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  189.                       .getService(Components.interfaces.nsIPrefBranch);
  190.           var oldVersionNum = prefs.getCharPref("extensions.ybookmarks@yahoo.version.number");
  191.                  yDebug.print("idManager.js::YBidManager::_isEngineInstalled()=>oldVersionNum= "+oldVersionNum,
  192.               YB_LOG_MESSAGE);
  193.    
  194.       }catch (e){
  195.           yDebug.print("idManager.js::YBidManager::_isEngineInstalled()=>Engine not installed",
  196.               YB_LOG_MESSAGE);
  197.       }               
  198.       if(newVersionNum == oldVersionNum){
  199.           installed = true;
  200.         yDebug.print("idManager.js::YBidManager::_isEngineInstalled()=>Engine installed",
  201.               YB_LOG_MESSAGE);
  202.          }
  203.          return installed;
  204.   },
  205.   
  206.   /**
  207.    *  Cancel the sync
  208.    *
  209.    */
  210.   _cancelSync : function() {
  211.   
  212.      this._syncService.cancelSync();
  213.      //this._localStoreService.removeAllTransactions(10);      
  214.   },
  215.   
  216.     /**
  217.    *  Remove bookmarks,bundles and transactions list.
  218.    *
  219.    */
  220.   _removeUsersBookmarksStore : function() {
  221.      this._localStoreService.deleteAllBookmarks(true);
  222.      this._localStoreService.clearBundles();
  223.      this._localStoreService.removeAllTransactions(10);
  224.      ybToolbar.refreshCurrentView();
  225.   },
  226.   
  227. /*  logoutWindowShown: function() {
  228.     var bundle, serviceName, str, elt, i;
  229.  
  230.     bundle = document.getElementById( "strbndl_idManager");
  231.  
  232.     str = bundle.getFormattedString( "extensions.ybookmarks.loggedOut.dialog.title", [ serviceName ] );
  233.     document.title = str;
  234.   },
  235. */  
  236.   onLogoutWindowAccept: function(event) {
  237.     var radio = document.getElementById("ybLoggedOut-radio");
  238.     var keepRadio = document.getElementById("ybLoggedOut-radio-keep");
  239.     if (radio.selectedItem == keepRadio) {
  240.       YBidManager.keepLocalContent();  
  241.     } else {
  242.       YBidManager.removeLocalContent();
  243.     }
  244.     return true;
  245.   },
  246.   
  247.  
  248.   keepLocalContent: function() {
  249.   try {
  250.     window.arguments[0].remove = false;
  251.   } catch(e) { //do nothing
  252.   }
  253.     //window.close();
  254.   },
  255.  
  256.   removeLocalContent: function() {
  257.   try {
  258.     window.arguments[0].remove = true;
  259.   } catch(e) {
  260.     // do nothing
  261.   }
  262.     //window.close();
  263.   },
  264.  
  265.   isUserLoggedIn: function() {    
  266.       try {
  267.           var silentMode = Components.classes["@mozilla.org/preferences-service;1"]
  268.                          .getService(Components.interfaces.nsIPrefBranch)
  269.                          .getBoolPref("extensions.ybookmarks@yahoo.delicious.silentlogout");
  270.         if(silentMode) {
  271.             return false;
  272.         }                 
  273.     } catch(e) {}
  274.     return (this._socialStoreService.getUserName() ? true : false);
  275.   },
  276.  
  277.   promptUserLogin: function() {
  278.       window.ybLoginWindowFlag = "";
  279.     window.openDialog( "chrome://ybookmarks/content/reqUserCred.xul",
  280.                        "ReqUserCred",
  281.                        "chrome,dialog,centerscreen,modal,resizable=no" );
  282.   },
  283.  
  284.   acctSetupDlgShown: function() {
  285.     var bundle = document.getElementById( "strbndl_idManager");
  286.  
  287.     var serviceName = deliciousService.getServiceName();
  288.     var str = bundle.getFormattedString( "extensions.ybookmarks.reqUserCred.message", [ serviceName ] );
  289.     ( document.getElementById( "desc_mainText" ) ).appendChild( document.createTextNode( str ) );
  290.     str = bundle.getFormattedString( "extensions.ybookmarks.reqUserCred.dialog.title", 
  291.                                      [ serviceName ] );
  292.     document.title = str;
  293.   },
  294.   
  295.   cancel: function() {
  296.     window.close();
  297.   },
  298.  
  299.   showAcctSetup: function() {
  300.     window.close();
  301.     ybookmarksUtils.openPreferences( "yb_account" );
  302.   },
  303.   
  304.   showLoginPage: function() {
  305.     window.opener.ybLoginWindowFlag = "display";
  306.     window.close();
  307.   },
  308.   
  309.   switchToClassicModeWarning: function(event) {
  310.       _userSelection = "cancel";        
  311.       window.openDialog( "chrome://ybookmarks/content/switchtoClassic.xul",
  312.                        "Switch To Classic",
  313.                        "chrome,dialog,centerscreen,modal,resizable=no");  
  314.                        
  315.     ////Disable the switch mode buttton.
  316.     var pref = Components.classes["@mozilla.org/preferences-service;1"].
  317.                  getService(Components.interfaces.nsIPrefBranch);
  318.     try {
  319.         var modeChange = pref.getCharPref("extensions.ybookmarks@yahoo.engine.set.mode");
  320.         if(modeChange == YB_EXTENSION_MODE_CLASSIC) {
  321.             var switchButton = document.getElementById("btn_classic_mode");
  322.             switchButton.disabled = true;
  323.         }
  324.     } catch(e){}
  325.     
  326.     //Restart
  327.     if(_userSelection == "restart") {
  328.         _userSelection = "cancel";
  329.         ybookmarksUtils._quit(true);        
  330.     }
  331.   },
  332.   
  333.   modeChangeToClassic : function() {
  334.     //set the mode pref value.    
  335.     var pref = Components.classes["@mozilla.org/preferences-service;1"].
  336.              getService(Components.interfaces.nsIPrefBranch);
  337.     pref.setCharPref("extensions.ybookmarks@yahoo.engine.set.mode", YB_EXTENSION_MODE_CLASSIC);     
  338.       //Keep/Remove bookmarks
  339.     var radio = document.getElementById("ybLoggedOut-radio");
  340.     var keepRadio = document.getElementById("ybLoggedOut-radio-keep");
  341.     if (radio.selectedItem == keepRadio) {
  342.     } else {    
  343.         YBidManager._cancelSync();
  344.         YBidManager._socialStoreService.disallowImportPolling();
  345.         YBidManager._removeUsersBookmarksStore();
  346.     }
  347.       var restartCheck = document.getElementById("ybRestartNow");
  348.     if(restartCheck.checked) {
  349.         //Restart hack
  350.         window.opener._userSelection = "restart";        
  351.         //ybookmarksUtils._quit(true);
  352.     }
  353.     return true;
  354.   },
  355.   
  356.   switchToStandardMode: function() {
  357.     var params = {out:null};
  358.     window.openDialog("chrome://ybookmarks/content/switchToRegMode.xul", "", "chrome,dialog,centerscreen,modal,resizable=no", params);
  359.     if(params.out) {               
  360.         //set the mode pref value and restart.
  361.         var pref = Components.classes["@mozilla.org/preferences-service;1"].
  362.                  getService(Components.interfaces.nsIPrefBranch);
  363.         pref.setCharPref("extensions.ybookmarks@yahoo.engine.set.mode", YB_EXTENSION_MODE_STANDARD);                
  364.         ybookmarksUtils.restartBrowser();
  365.     }
  366.     return true;         
  367.   }
  368.   
  369. };
  370.  
  371.